package com.hornet.manager.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.hornet.commons.web.BaseController;
import com.hornet.manager.weixin.constant.WeixinConstant;
import com.hornet.manager.weixin.json.AccessToken;
import com.hornet.manager.weixin.json.qr.Qr;
import com.hornet.manager.weixin.json.qr.QrTicket;
import com.hornet.manager.weixin.json.qr.Scene;
import com.hornet.manager.weixin.util.HttpUtil;

@Controller
@RequestMapping("/other")
public class OtherController extends BaseController {
	
	/**
	 * UI icons
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/ui_icons", method=RequestMethod.GET)
	public String ui_icons(HttpServletRequest request) {
		return viewName("ui-icons") ;
	}
	
	/**
	 * 百度在线编辑器
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/ueditor", method=RequestMethod.GET)
	public String ueditor(HttpServletRequest request) {
		return viewName("ueditor") ;
	}
	@RequestMapping(value="/ueditor_add", method=RequestMethod.POST)
	public @ResponseBody String ueditor_add(String content) {
		return content ;
	}
	
	@RequestMapping(value="/wx_qr", method=RequestMethod.GET)
	public String wx_qr(Model model) throws ClientProtocolException, IOException {
		
		model.addAttribute("qr", getTicket()) ;
		
		return viewName("wx_qr") ;
	}
	
	public QrTicket getTicket() throws ClientProtocolException, IOException  {
		AccessToken ac = access_token();
		String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST" ;
		url = url.replace("TOKENPOST", ac.getAccess_token());
		
		Map<String, Object> scene = new HashMap<String, Object>() ;
		scene.put("scene", new Scene("http://yhaoquan.imwork.net/")) ;
		
		Qr qr = new Qr();
		qr.setAction_name("QR_LIMIT_STR_SCENE");
		qr.setExpire_seconds(4000);
		qr.setAction_info(scene);
		
		String json = JSON.toJSONString(qr) ;
		System.out.println(json);
		
		String doPost = HttpUtil.doPost(url, json) ;
		System.out.println(doPost);
		
		QrTicket qrTicket = JSON.parseObject(doPost, QrTicket.class) ;
		//qrTicket.setUrl("http://yhaoquan.imwork.net");
		
		return qrTicket ;
	}
	
	public AccessToken access_token() throws ClientProtocolException, IOException {
		CloseableHttpClient client = HttpClients.createDefault() ;
		
		String url = WeixinConstant.ACCESS_TOKEN_URL ;
		url = url.replace("APPID", WeixinConstant.APPID);
		url = url.replace("APPSECRET", WeixinConstant.APPSECRET);
		
		HttpGet get = new HttpGet(url) ;
		
		CloseableHttpResponse resp = client.execute(get) ;
		int sc = resp.getStatusLine().getStatusCode() ;
		if(sc >= 200 && sc< 300) {
			JSON json = (JSON) JSON.parse(EntityUtils.toString(resp.getEntity())) ;
			AccessToken access_token = JSON.toJavaObject(json, AccessToken.class) ;
			
			return access_token ;
		} else {
			System.out.println("发生错误。。。");
		}
		return null;
	}
	
}
